// gjw/pages/dingzhi/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    post_list:[
      {
        img:'http://img2.imgtn.bdimg.com/it/u=4011900715,2701798727&fm=27&gp=0.jpg',
        name:'疯子',
        detail: '客观硬件对战术使用的限制。在联合国军行动时，敏锐意识到沙漠作战如同海洋作战，在卫星＋武装直升机近地测绘的帮助下，能辨明方向掌握主动。所以联军部队能在浩瀚大漠中快速、有目的的推进。相比之下，伊拉克军队只能被迫建立大漠中的支撑点，并以巡逻＋坦克半埋在沙丘反斜面（国内无知军文单纯说是半埋，没提反斜面的大有人在，懒得喷）的战术以期最大限度上在极近距离行成遭遇战，拉低双方装备、训练上的巨大差距。但收效甚微，其中较成功的算麦迪娜岭战斗，但只是多阻挡了几个小时而已。',
        time:'2018-08-17 11:00',
        view:20,
        imgs:'http://img3.imgtn.bdimg.com/it/u=3145817652,3187012951&fm=27&gp=0.jpg!http://img1.imgtn.bdimg.com/it/u=350021195,425620616&fm=27&gp=0.jpg!http://img1.imgtn.bdimg.com/it/u=2552631884,3468026667&fm=27&gp=0.jpg!http://img0.imgtn.bdimg.com/it/u=998183770,1815484616&fm=27&gp=0.jpg',
        title:'定制模块标题',
        tel:'13277915655',
        type:1
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      dz_id:options.id
    })
    app.location_url(this)
    app.util.request({
      url: 'entry/wxapp/primoney',
      success: res => {
        that.setData({
          cost: res.data.res
        })
      }
		})
		getApp().util.request({
			url: 'entry/wxapp/Repucostiwantut',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						iwantrestups: res.data.restups,
						iwantresttops: res.data.resttops
					})
				}
			}
		})

    that.see()
    that.setData({
      openid:wx.getStorageSync('openid')
    })
    app.util.request({
      url: 'entry/wxapp/Lookpro',
      data: {
        id: options.id,
        openid: wx.getStorageSync('openid')
      },
      success: res => {
        
        if(res.data.code=='500'){
          that.detail_info(options.id,0)
        }else{
          that.detail_info(options.id,1)
        }
      }
    })
  },


  // 浏览
  see: function (e) {
    var that=this
		var openid = wx.getStorageSync('openid')
    var id = that.data.dz_id
    app.util.request({
      url:'entry/wxapp/Priview',
      data:{
        openid:openid,id:id
      },
      success:function(res){
        
        that.data.post_list.view = res.data.res
        that.setData({
          post_list: that.data.post_list
        })      
      }
    })
  },


  detail_info:function(id,statu){
      var that = this
      app.util.request({
        url:'entry/wxapp/Pridetail',
        data:{
          id:id
        },
        success:res=>{
          
          var post_list = res.data.res
          if (statu == 0 && post_list.type == 1 && post_list.openid != wx.getStorageSync('openid')){
            post_list.phone = post_list.phone.slice(0, 3) + '*****' + post_list.phone.slice(7, 11)
          }
          post_list.img = post_list.img.split(",")
          that.setData({
            post_list: post_list,
            statu: statu
          })
          that.see()
        }
      })
  },
  phone:function(e){
    var that = this
    var post_list = that.data.post_list
		if (post_list.phone.indexOf("*") == -1) {
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.tel //仅为示例，并非真实的电话号码
      })
    }else{
     wx.showModal({
       title: '提示',
       content: '您需要付款之后才能查看',
       success:function(res){
         if(res.confirm){
           app.util.request({
             url: 'entry/wxapp/Lookfee',
             data: {
               openid: wx.getStorageSync('openid'),
               lookfee: that.data.cost.lookfee
             },
             success: res => {
               
               wx.requestPayment({
                 'timeStamp': res.data.timeStamp,
                 'nonceStr': res.data.nonceStr,
                 'package': res.data.package,
                 'signType': 'MD5',
                 'paySign': res.data.paySign,
                 'success': function (res) {
                   that.change_status()
                 },
                 'fail': function (res) {
                 }
               })
             }
           })
         }else if(res.cancel){}
       }
     })
      
    }
  },

  

  // 改变订单状态
  change_status: function (e) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Lookpaysuccess',
      data: {
        openid: wx.getStorageSync('openid'),
        id: that.data.post_list.id
      },
      success: Res => {
        
        wx.redirectTo({
          url: 'detail?id='+that.data.post_list.id,
        })
      }
    })
  },
  // 置顶帖子
  setTop:function(e){
		var that=this
		var openid = wx.getStorageSync('openid')
		if(that.data.cost.topfee>0){
			if(that.data.iwantresttops>0){
				app.util.request({
					url: 'entry/wxapp/pritopsuccess',
					data: {
						id: that.data.post_list.id, openid: openid, topu: 1
					},
					success: function (res) {

						if (res.data.code == 200) {

							wx.showToast({
								icon: "none",
								title: '置顶成功',
							})
						} else {
							wx.showToast({
								icon: "none",
								title: '置顶失败',
							})
						}
					}
				})
			}else{
				app.util.request({
					url: 'entry/wxapp/Pritoppay',
					data: {
						openid: wx.getStorageSync('openid')
					},
					success: res => {
						wx.requestPayment({
							'timeStamp': res.data.timeStamp,
							'nonceStr': res.data.nonceStr,
							'package': res.data.package,
							'signType': 'MD5',
							'paySign': res.data.paySign,
							'success': function (res) {
								app.util.request({
									url: 'entry/wxapp/Pritopsuccess',
									data: {
										id: that.data.post_list.id
									},
									success: Res => {
										wx.showToast({
											title: '置顶成功',
										})
									}
								})
							},
							'fail': function (res) {
							}
						})
					}
				})
			}
		}else{
			app.util.request({
				url: 'entry/wxapp/pritopsuccess',
				data: {
					id: that.data.post_list.id
				},
				success: function (res) {

					if (res.data.code == 200) {

						wx.showToast({
							icon: "none",
							title: '置顶成功',
						})
					} else {
						wx.showToast({
							icon: "none",
							title: '置顶失败',
						})
					}
				}
			})
		}
  },
  // 置顶帖子
	recommed: function (e) {
		var that = this
		var openid = wx.getStorageSync('openid')
		if (that.data.cost.upfee > 0) {
			if (that.data.iwantrestups > 0) {
				app.util.request({
					url: 'entry/wxapp/priupsuccess',
					data: {
						id: that.data.post_list.id, openid: openid, upu: 1
					},
					success: function (res) {
						if (res.data.code == 200) {
							wx.showToast({
								icon: "none",
								title: '推荐成功',
							})
							that.data.post_list.upfee=1
							that.setData({
								post_list: that.data.post_list
							})
						} else {
							wx.showToast({
								icon: "none",
								title: '推荐失败',
							})
						}
					}
				})
			} else {
				app.util.request({
					url: 'entry/wxapp/Priuppay',
					data: {
						openid: wx.getStorageSync('openid')
					},
					success: res => {
						wx.requestPayment({
							'timeStamp': res.data.timeStamp,
							'nonceStr': res.data.nonceStr,
							'package': res.data.package,
							'signType': 'MD5',
							'paySign': res.data.paySign,
							'success': function (res) {
								app.util.request({
									url: 'entry/wxapp/Priupsuccess',
									data: {
										id: that.data.post_list.id
									},
									success: Res => {

										wx.showToast({
											title: '推荐成功',
										})
									}
								})
							},
							'fail': function (res) {
							}
						})
					}
				})
			}
		}else{
			app.util.request({
				url: 'entry/wxapp/priupsuccess',
				data: {
					id: that.data.post_list.id
				},
				success: function (res) {
					if (res.data.code == 200) {
						wx.showToast({
							icon: "none",
							title: '推荐成功',
						})
					} else {
						wx.showToast({
							icon: "none",
							title: '推荐失败',
						})
					}
				}
			})
		}
  },


    // 查看大图
  previewImage: function (e) {
    var that = this
    var url = that.data.url   // {{siteUrl}}attachment/wff/    地址前缀
    var urls = []
    var current
    var index = e.currentTarget.dataset.index   //当前点击的下标
    var pictures = that.data.post_list.img
    for (let i in pictures) {
      urls.push(url + 'attachment/' + pictures[i])
    }
    wx.previewImage({
      current: url + 'attachment/' + pictures[index],
      urls: urls
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})